// CameraSubDlg.cpp : 实现文件
// implementation file

#include "stdafx.h"
#include "MultiCamerasTool.h"
#include "CameraSubDlg.h"

#include <sstream>

// CCameraSubDlg 对话框
// CCameraSubDlg dialog

extern ::CString getParseLanguageString(::CString strOrl);
#define STD_WIDTH (960)
#define STD_HEIGHT (529)
#define STD_FONT_SIZE (100)

double g_widthRate = 0.0;
double g_heightRate = 0.0;

IMPLEMENT_DYNAMIC(CCameraSubDlg, CDialog)

CCameraSubDlg::CCameraSubDlg(CWnd* pParent)
	: CDialog(CCameraSubDlg::IDD, pParent)
	, m_valExposureTime(0)
	, m_valGain(0)
	, m_valFrameRate(0)
	, m_bFirstTrigger(true)
	, m_pForceIpDlg(NULL)
{

}

CCameraSubDlg::~CCameraSubDlg()
{
	if ( NULL != m_pForceIpDlg )
	{
		delete m_pForceIpDlg;
	}
}

BOOL CCameraSubDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 根据目前配置的语言初始化界面文本显示 | Initialize the text display of the interface according to the currently configured language
	{
		this->SetWindowText(getParseLanguageString(_T("_MAINFORM_TEXT")));
		GetDlgItem(IDC_STATIC_CAMERA)->SetWindowText(getParseLanguageString(_T("_MAINFORM_CAMERA")));
		GetDlgItem(IDC_STATIC_EXPOSURE)->SetWindowText(getParseLanguageString(_T("_MAINFORM_EXPOSURE")));
		GetDlgItem(IDC_STATIC_EXPOSURE_AUTO)->SetWindowText(getParseLanguageString(_T("_MAINFORM_EXPOSE_AUTO")));
		GetDlgItem(IDC_STATIC_EXPOSURE_MODE)->SetWindowText(getParseLanguageString(_T("_MAINFORM_EXPOSEMODE")));
		GetDlgItem(IDC_STATIC_EXPOSURE_TIME)->SetWindowText(getParseLanguageString(_T("_MAINFORM_EXPOSEVALUE")));
		GetDlgItem(IDC_STATIC_GAIN)->SetWindowText(getParseLanguageString(_T("_MAINFORM_GAIN")));
		GetDlgItem(IDC_STATIC_GAIN_AUTO)->SetWindowText(getParseLanguageString(_T("_MAINFORM_GAIN_AUTO")));
		GetDlgItem(IDC_STATIC_GAIN_RAW)->SetWindowText(getParseLanguageString(_T("_MAINFORM_GAINVALUE"))); 
		GetDlgItem(IDC_STATIC_FRAME_RATE)->SetWindowText(getParseLanguageString(_T("_MAINFORM_FRAMERATE")));
		GetDlgItem(IDC_STATIC_ACQUISITION_FRAMERATE)->SetWindowText(getParseLanguageString(_T("_MAINFORM_FRAMERATE_VALUE")));
		GetDlgItem(IDC_STATIC_IMAGE_FORMAT)->SetWindowText(getParseLanguageString(_T("_MAINFORM_IMAGEFORMAT")));
		GetDlgItem(IDC_STATIC_PIXEL_FORMAT)->SetWindowText(getParseLanguageString(_T("_MAINFORM_FORMAT")));
		GetDlgItem(IDC_STATIC_TRIGGER)->SetWindowText(getParseLanguageString(_T("_MAINFORM_SOFT_TRIGGER")));
		GetDlgItem(IDC_STATIC_TRIGGER_MODE)->SetWindowText(getParseLanguageString(_T("_MAINFORM_TRIGGER_MODE")));
		GetDlgItem(IDC_STATIC_TRIGGER_SOFTWARE)->SetWindowText(getParseLanguageString(_T("_MAINFORM_TRIGGER")));
		GetDlgItem(IDC_BUTTON_TRIGGER)->SetWindowText(getParseLanguageString(_T("_MAINFORM_SOFTWARE_TRIGGER")));
		GetDlgItem(IDC_STATIC_CAMERA_INFO)->SetWindowText(getParseLanguageString(_T("_MAINFORM_CAMERA_IMFORMATION")));
		GetDlgItem(IDC_STATIC_GATEWAY)->SetWindowText(getParseLanguageString(_T("_MAINFORM_GATEWAY")));
		GetDlgItem(IDC_STATIC_MASK)->SetWindowText(getParseLanguageString(_T("_MAINFORM_SUBNET_MASK")));
		GetDlgItem(IDC_STATIC_IP)->SetWindowText(getParseLanguageString(_T("_MAINFORM_IP_ADDRESS")));
		GetDlgItem(IDC_STATIC_FORCE_IP)->SetWindowText(getParseLanguageString(_T("_MAINFORM_SETTING")));
		GetDlgItem(IDC_BUTTON_FORCEIP)->SetWindowText(getParseLanguageString(_T("_MAINFORM_MODIFY_IP")));
		GetDlgItem(IDC_BUTTON_CONNECT)->SetWindowText(getParseLanguageString(_T("_MAINFORM_CONNECT")));
		GetDlgItem(IDC_BUTTON_DISCONNECT)->SetWindowText(getParseLanguageString(_T("_MAINFORM_DISCONNECT")));
		GetDlgItem(IDC_BUTTON_PLAY)->SetWindowText(getParseLanguageString(_T("_MAINFORM_PLAY")));
		GetDlgItem(IDC_BUTTON_STOPPLAY)->SetWindowText(getParseLanguageString(_T("_MAINFORM_STOP_PLAY")));
		GetDlgItem(IDC_BUTTON_SAVECONFIG)->SetWindowText(getParseLanguageString(_T("_MAINFORM_SAVE_CONFIG")));
	}

	MoveWindow(m_rectDlg);

	CRect rectSubDlg;
	GetClientRect(rectSubDlg);

	g_widthRate = (double)rectSubDlg.BottomRight().x / STD_WIDTH;
	g_heightRate = (double)rectSubDlg.BottomRight().y / STD_HEIGHT;

	GetDlgItem(IDC_STATIC_CAMERA)->MoveWindow( 
		10 * g_widthRate,
		10 * g_heightRate,
		(int)((STD_WIDTH - 20) * g_widthRate),
		(int)((STD_HEIGHT - 20) * g_heightRate));

	for (int iItemId = IDC_STATIC_EXPOSURE; iItemId <= IDC_STATIC_CAMERA_INFO; ++iItemId)
	{
		CWnd* pWnd = GetDlgItem(iItemId);
		if ( NULL == pWnd )
		{
			continue;
		}

		// 根据屏幕大小缩放控件
		// scale controls based on screen size
		CRect rectTmp;
		pWnd->GetWindowRect(&rectTmp);
		ScreenToClient(&rectTmp);		

		CRect rectAdaptor;
		rectAdaptor.TopLeft().x = (int)((rectTmp.TopLeft().x) * g_widthRate);
		rectAdaptor.TopLeft().y = (int)((rectTmp.TopLeft().y) * g_heightRate);
		rectAdaptor.BottomRight().x = (int)((rectTmp.BottomRight().x) * g_widthRate);
		rectAdaptor.BottomRight().y = (int)((rectTmp.BottomRight().y) * g_heightRate);

		pWnd->MoveWindow(rectAdaptor);

		// 根据屏幕大小缩放字体
		// scale font based on screen size
		if ( IDC_STATIC_EXPOSURE == iItemId )
		{
			double iFontRate = g_widthRate;
			if ( g_heightRate < g_widthRate )
			{
				iFontRate = g_heightRate;
			}

			int iFontSize = (int)(STD_FONT_SIZE * iFontRate);
			m_font.CreatePointFont(iFontSize, "");
		}

		if ( IDC_SPIN_EXPOSURE != iItemId && 
			IDC_SPIN_GAIN != iItemId && 
			IDC_SPIN_FRAMERATE != iItemId )
		{
			pWnd->SetFont(&m_font);
		}		
	}

	// 禁用全部控件
	// disable all controls
	enableWindowAll(FALSE);

	return TRUE;
}

void CCameraSubDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_EXPOSUREAUTO, m_cmbExposureAuto);
	DDX_Control(pDX, IDC_COMBO_EXPOSUREMODE, m_cmbExposureMode);
	DDX_Control(pDX, IDC_EDIT_EXPOSURE, m_editExposure);
	DDX_Control(pDX, IDC_SPIN_EXPOSURE, m_spinExposure);
	DDX_Control(pDX, IDC_COMBO_GAINAUTO, m_cmbGainAuto);
	DDX_Control(pDX, IDC_EDIT_GAIN, m_editGain);
	DDX_Control(pDX, IDC_SPIN_GAIN, m_spinGain);
	DDX_Control(pDX, IDC_EDIT_FRAMERATE, m_editFrameRate);
	DDX_Control(pDX, IDC_SPIN_FRAMERATE, m_spinFrameRate);
	DDX_Control(pDX, IDC_COMBO_IMAGEFORMAT, m_cmbImageFormat);
	DDX_Control(pDX, IDC_COMBO_TRIGGERMODE, m_cmbTriggerMode);
	DDX_Control(pDX, IDC_BUTTON_TRIGGER, m_btnTrigger);
	DDX_Control(pDX, IDC_BUTTON_FORCEIP, m_btnForceIp);
	DDX_Control(pDX, IDC_BUTTON_CONNECT, m_btnConnect);
	DDX_Control(pDX, IDC_BUTTON_DISCONNECT, m_btnDisconnect);
	DDX_Control(pDX, IDC_BUTTON_PLAY, m_btnPlay);
	DDX_Control(pDX, IDC_BUTTON_STOPPLAY, m_btnStopPlay);
	DDX_Control(pDX, IDC_BUTTON_SAVECONFIG, m_btnSaveConfig);
	DDX_Control(pDX, IDC_LIST_DISPLAY, m_lbShowWindow);
	DDX_Text(pDX, IDC_EDIT_EXPOSURE, m_valExposureTime);
	DDX_Text(pDX, IDC_EDIT_GAIN, m_valGain);
	DDX_Text(pDX, IDC_EDIT_FRAMERATE, m_valFrameRate);
	DDX_Control(pDX, IDC_EDIT_GATEWAY, m_editGateway);
	DDX_Control(pDX, IDC_EDIT_MASK, m_editMask);
	DDX_Control(pDX, IDC_EDIT_IP, m_editIp);
}

void CCameraSubDlg::onCameraDisconnect()
{
	PostMessage(WM_MY_DISCONNECT_MESSAGE, NULL, NULL);
}

void CCameraSubDlg::setRect(CRect rect)
{
	m_rectDlg = rect;
}

void CCameraSubDlg::setCamera(const Dahua::GenICam::ICameraPtr& sptrCamera)
{
	// 激活子界面
	// activate sub interface
	enableWindowAll(TRUE);

	// 控制属性控件状态
	// control property control state
	enableProperties(FALSE);	

	// 控制相关按钮状态
	// control related button status
	m_btnDisconnect.EnableWindow(FALSE);
	m_btnPlay.EnableWindow(FALSE);
	m_btnStopPlay.EnableWindow(FALSE);
	m_btnSaveConfig.EnableWindow(FALSE);
	m_btnTrigger.EnableWindow(FALSE);
	m_editGateway.EnableWindow(FALSE);
	m_editMask.EnableWindow(FALSE);
	m_editIp.EnableWindow(FALSE);

	// 设置相机对象智能指针
	// set camera object smart pointer
	m_sptrMvCamera = Dahua::CMvCameraPtr(new Dahua::CMvCamera(sptrCamera));

	// 显示相机信息
	// display camera information
	initCameraIpInfo();
}

void CCameraSubDlg::enableProperties(BOOL bEnable)
{
	if ( FALSE == bEnable )
	{
		m_cmbExposureAuto.ResetContent();
		m_cmbExposureMode.ResetContent();
		m_editExposure.SetWindowText(_T("0"));
		m_cmbGainAuto.ResetContent();
		m_editGain.SetWindowText(_T("0"));
		m_editFrameRate.SetWindowText(_T("0"));
		m_cmbImageFormat.ResetContent();
		m_cmbTriggerMode.ResetContent();
	}

	m_cmbExposureAuto.EnableWindow(bEnable);
	m_cmbExposureMode.EnableWindow(bEnable);
	m_editExposure.EnableWindow(bEnable);
	m_spinExposure.EnableWindow(bEnable);
	m_cmbGainAuto.EnableWindow(bEnable);
	m_editGain.EnableWindow(bEnable);
	m_spinGain.EnableWindow(bEnable);
	m_editFrameRate.EnableWindow(bEnable);
	m_spinFrameRate.EnableWindow(bEnable);
	m_cmbImageFormat.EnableWindow(bEnable);
	m_cmbTriggerMode.EnableWindow(bEnable);
}

void CCameraSubDlg::enableWindowAll(BOOL bEnbale)
{
	for (int iItemId = IDC_STATIC_CAMERA; iItemId <= IDC_STATIC_CAMERA_INFO; ++iItemId)
	{
		CWnd* pWnd = GetDlgItem(iItemId);
		if ( NULL == pWnd )
		{
			continue;
		}

		pWnd->EnableWindow(bEnbale);
	}
}

void CCameraSubDlg::initParamProperty()
{
	Dahua::CMvCamera::ParamControlSetPtr sptrParamCtrlSet = m_sptrMvCamera->getParamControl();
	
	// ExposureAuto
	Dahua::GenICam::CEnumNode sptrExposureAuto = sptrParamCtrlSet->_sptrAcquistionCtrl->exposureAuto();
	initComboBoxTypeProperty(sptrExposureAuto, &m_cmbExposureAuto);

	// ExposureMode
	Dahua::GenICam::CEnumNode sptrExposureMode = sptrParamCtrlSet->_sptrAcquistionCtrl->exposureMode();
	initComboBoxTypeProperty(sptrExposureMode, &m_cmbExposureMode);

	// ExposureTime
	Dahua::GenICam::CDoubleNode sptrExposureTime = sptrParamCtrlSet->_sptrAcquistionCtrl->exposureTime();
	initFloatSpinBoxTypeProperty(sptrExposureTime, &m_editExposure, &m_spinExposure);

	// GainAuto
	Dahua::GenICam::CEnumNode sptrGainAuto = sptrParamCtrlSet->_sptrAnalogCtrl->gainAuto();
	initComboBoxTypeProperty(sptrGainAuto, &m_cmbGainAuto);

	// GainRaw
	Dahua::GenICam::CDoubleNode sptrGainRaw = sptrParamCtrlSet->_sptrAnalogCtrl->gainRaw();
	initFloatSpinBoxTypeProperty(sptrGainRaw, &m_editGain, &m_spinGain);

	// AcquistionFrameRate
	Dahua::GenICam::CDoubleNode sptrFrameRate = sptrParamCtrlSet->_sptrAcquistionCtrl->acquisitionFrameRate();
	initFloatSpinBoxTypeProperty(sptrFrameRate, &m_editFrameRate, &m_spinFrameRate);

	// PixelFormat
	Dahua::GenICam::CEnumNode sptrPixelFormat = sptrParamCtrlSet->_sptrImageFormatCtrl->pixelFormat();
	initComboBoxTypeProperty(sptrPixelFormat, &m_cmbImageFormat);

	// TriggerMode
	Dahua::GenICam::CEnumNode sptrTriggerMode = sptrParamCtrlSet->_sptrAcquistionCtrl->triggerMode();
	initComboBoxTypeProperty(sptrTriggerMode, &m_cmbTriggerMode);
}

void CCameraSubDlg::initComboBoxTypeProperty(Dahua::GenICam::CEnumNode sptrEnumNode, CComboBox* pCmbProperty)
{
	if ( !sptrEnumNode.isValid() || 
		NULL == pCmbProperty )
	{
		return;
	}

	pCmbProperty->Clear();

	Dahua::Infra::CString valEnumSymbol;
	sptrEnumNode.getValueSymbol(valEnumSymbol);
	Dahua::Infra::TVector<Dahua::Infra::CString> vecEnumEntrys = sptrEnumNode.getEnumSymbolList();
	for (uint32_t iIndex = 0; iIndex < vecEnumEntrys.size(); ++iIndex)
	{
		pCmbProperty->InsertString(iIndex,(LPCTSTR)vecEnumEntrys[iIndex].c_str());
		if ( valEnumSymbol == vecEnumEntrys[iIndex] )
		{
			pCmbProperty->SetCurSel(iIndex);
		}
	}

	BOOL bEnable = sptrEnumNode.isWriteable() ? TRUE : FALSE;
	pCmbProperty->EnableWindow(bEnable);
}

void CCameraSubDlg::initFloatSpinBoxTypeProperty(Dahua::GenICam::CDoubleNode sptrDoubleNode, CEdit* pEditProperty, CSpinButtonCtrl* pSpinBtn)
{
	if ( !sptrDoubleNode.isValid() ||
		NULL == pEditProperty ||
		NULL == pSpinBtn )
	{
		return;
	}

	double fltVal = 0.0;
	sptrDoubleNode.getValue(fltVal);

	char szBuf[24] = {0};
	sprintf_s( szBuf, 24, "%5f", fltVal );

	pEditProperty->SetWindowText((LPCTSTR)szBuf);
	UpdateData(TRUE);

	BOOL bEnable = sptrDoubleNode.isWriteable() ? TRUE : FALSE;
	pEditProperty->EnableWindow(bEnable);
	pSpinBtn->EnableWindow(bEnable);
}

void CCameraSubDlg::initCameraIpInfo()
{
	if ( NULL == m_sptrMvCamera )
	{
		return;
	}

	Dahua::CMvCamera::NetAddressInfoPtr sptrCameraIpInfo = m_sptrMvCamera->getGigECameraInfo();
	if ( NULL != sptrCameraIpInfo )
	{
#ifdef UNICODE
		std::wstring strGateAddr = StringToWString(sptrCameraIpInfo->_strGatewayAddr);
		std::wstring strMaskAddr = StringToWString(sptrCameraIpInfo->_strMaskAddr);
		std::wstring strIpAddr = StringToWString(sptrCameraIpInfo->_strIpAddr);
#else
		std::string strGateAddr = sptrCameraIpInfo->_strGatewayAddr;
		std::string strMaskAddr = sptrCameraIpInfo->_strMaskAddr;
		std::string strIpAddr = sptrCameraIpInfo->_strIpAddr;
#endif // UNICODE
		
		m_editGateway.SetWindowText(strGateAddr.c_str());
		m_editMask.SetWindowText(strMaskAddr.c_str());
		m_editIp.SetWindowText(strIpAddr.c_str());
	}
	else
	{
		// U3V相机无法修改ip
		// U3v camera cannot modify IP
		m_btnForceIp.EnableWindow(false);
	}
}

void CCameraSubDlg::procSelchangeCombo(Dahua::GenICam::CEnumNode sptrEnumNode, CComboBox* pCmbProperty)
{
	// 获取界面数据
	// get interface data
	UpdateData(TRUE);

	if ( !sptrEnumNode.isValid() ||
		NULL == pCmbProperty )
	{
		return;
	}

	TCHAR szBuf[24] = {0};
	int iIndex = pCmbProperty->GetCurSel();
	pCmbProperty->GetLBText(iIndex, szBuf);

	if ( sptrEnumNode.isWriteable() )
	{
#ifdef UNICODE
		std::string strValSymbol = WStringToString(szBuf);
#else
		std::string strValSymbol = szBuf;
#endif // UNICODE

		sptrEnumNode.setValueBySymbol(strValSymbol.c_str());
	}
	else
	{
		AfxMessageBox(_T("This parameter is not writeable!"));
	}
}

void CCameraSubDlg::procDoubleSpinCtrl(LPNMUPDOWN pNMUpDown, double& valEdit, CEdit* pCtrlEdit)
{
	UpdateData( TRUE );

	// +
	if ( pNMUpDown->iDelta == -1 )
	{
		valEdit += 1;	
	}
	// -
	else if ( pNMUpDown->iDelta == 1 )
	{
		valEdit -= 1;
	}

	// 数字转字符串
	// Number to string
	char szBuf[24] = {0};
	sprintf_s(szBuf, 24, "%5f", valEdit);

	// 设置控件值
	// set control value
	pCtrlEdit->SetWindowText((LPCTSTR)szBuf);

	UpdateData( FALSE );
}

bool CCameraSubDlg::procTriggerMode( const char* pMode )
{
	Dahua::CMvCamera::ParamControlSetPtr sptrParamCtrlSet = m_sptrMvCamera->getParamControl();
	if ( NULL == sptrParamCtrlSet )
	{
		return false;
	}

	// 获取TriggerMode节点对象
	// get trigger mode node object
	Dahua::GenICam::CEnumNode sptrTriggerMode = sptrParamCtrlSet->_sptrAcquistionCtrl->triggerMode();
	if ( !sptrTriggerMode.isValid() )
	{
		return false;
	}

	// 获取TriggerSelector节点对象
	// Get trigger Selector node object
	Dahua::GenICam::CEnumNode sptrTriggerSelector = sptrParamCtrlSet->_sptrAcquistionCtrl->triggerSelector();
	if ( !sptrTriggerSelector.isValid() )
	{
		return false;
	}

	// 设置AcquisitionStart TriggerMode值
	// set AcquisitionStart TriggerMod vlue
	bool bRet = sptrTriggerSelector.setValueBySymbol("AcquisitionStart");
	if ( !bRet )
	{
		return false;
	}

	bRet = sptrTriggerMode.setValueBySymbol(pMode);
	if ( !bRet )
	{
		return false;
	}

	// 设置FrameStart TriggerMode值
	// set FrameStart TriggerMode value
	bRet = sptrTriggerSelector.setValueBySymbol("FrameStart");
	if ( !bRet )
	{
		return false;
	}

	bRet = sptrTriggerMode.setValueBySymbol(pMode);
	if ( bRet )
	{
		if ( std::string(pMode) == "On" )
		{
			if ( NULL != m_sptrMvCamera )
			{
				m_sptrMvCamera->enableRateCtrl(false);
			}			
		}
		else
		{
			if ( NULL != m_sptrMvCamera )
			{
				m_sptrMvCamera->enableRateCtrl(true);
			}	
		}
	}

	return bRet;
}

BEGIN_MESSAGE_MAP(CCameraSubDlg, CDialog)
	ON_BN_CLICKED(IDC_BUTTON_CONNECT, &CCameraSubDlg::OnBnClickedButtonConnect)
	ON_BN_CLICKED(IDC_BUTTON_DISCONNECT, &CCameraSubDlg::OnBnClickedButtonDisconnect)
	ON_BN_CLICKED(IDC_BUTTON_PLAY, &CCameraSubDlg::OnBnClickedButtonPlay)
	ON_BN_CLICKED(IDC_BUTTON_STOPPLAY, &CCameraSubDlg::OnBnClickedButtonStopplay)
	ON_BN_CLICKED(IDC_BUTTON_SAVECONFIG, &CCameraSubDlg::OnBnClickedButtonSaveconfig)
	ON_CBN_SELCHANGE(IDC_COMBO_EXPOSUREAUTO, &CCameraSubDlg::OnCbnSelchangeComboExposureauto)
	ON_CBN_SELCHANGE(IDC_COMBO_EXPOSUREMODE, &CCameraSubDlg::OnCbnSelchangeComboExposuremode)
	ON_CBN_SELCHANGE(IDC_COMBO_GAINAUTO, &CCameraSubDlg::OnCbnSelchangeComboGainauto)
	ON_CBN_SELCHANGE(IDC_COMBO_IMAGEFORMAT, &CCameraSubDlg::OnCbnSelchangeComboImageformat)
	ON_CBN_SELCHANGE(IDC_COMBO_TRIGGERMODE, &CCameraSubDlg::OnCbnSelchangeComboTriggermode)
	ON_EN_UPDATE(IDC_EDIT_EXPOSURE, &CCameraSubDlg::OnEnUpdateEditExposure)
	ON_EN_UPDATE(IDC_EDIT_GAIN, &CCameraSubDlg::OnEnUpdateEditGain)
	ON_EN_UPDATE(IDC_EDIT_FRAMERATE, &CCameraSubDlg::OnEnUpdateEditFramerate)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_EXPOSURE, &CCameraSubDlg::OnDeltaposSpinExposure)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_GAIN, &CCameraSubDlg::OnDeltaposSpinGain)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_FRAMERATE, &CCameraSubDlg::OnDeltaposSpinFramerate)
	ON_BN_CLICKED(IDC_BUTTON_TRIGGER, &CCameraSubDlg::OnBnClickedButtonTrigger)
	ON_BN_CLICKED(IDC_BUTTON_FORCEIP, &CCameraSubDlg::OnBnClickedButtonForceip)
	ON_MESSAGE(WM_MY_DISCONNECT_MESSAGE, &CCameraSubDlg::OnMyDisconnectMsg)
	ON_WM_SIZE()
END_MESSAGE_MAP()


// CCameraSubDlg 消息处理程序
// CCameraSubDlg message processing program
void CCameraSubDlg::OnBnClickedButtonConnect()
{
	if ( NULL == m_sptrMvCamera )
	{
		AfxMessageBox(_T("Connect failed, camera is null!"));
		return;
	}

	bool bOpenRet = m_sptrMvCamera->open(this);	
	if ( !bOpenRet )
	{
		// 处理连接失败
		// processing connect failure
		AfxMessageBox(_T("Connect failed!"));
		return;
	}	

	// 初始化相机属性
	// initialize camera properties
	initParamProperty();

	// 控制属性控件状态
	// control property control state
	enableProperties(TRUE);

	// 控制相关按钮状态
	// Control related button status
	m_btnConnect.EnableWindow(FALSE);
	m_btnDisconnect.EnableWindow(TRUE);
	m_btnPlay.EnableWindow(TRUE);
	m_btnSaveConfig.EnableWindow(TRUE);
}

void CCameraSubDlg::OnBnClickedButtonDisconnect()
{
	if ( NULL != m_sptrMvCamera )
	{
		OnBnClickedButtonStopplay();
		m_sptrMvCamera->close();
	}

	// 控制属性控件状态
	// Control property control edit status
	enableProperties(FALSE);

	// 控制相关按钮状态
	// Control related button status
	m_btnConnect.EnableWindow(TRUE);
	m_btnDisconnect.EnableWindow(FALSE);
	m_btnPlay.EnableWindow(FALSE);
	m_btnStopPlay.EnableWindow(FALSE);
	m_btnSaveConfig.EnableWindow(FALSE);
}

void CCameraSubDlg::OnBnClickedButtonPlay()
{
	if ( NULL != m_sptrMvCamera )
	{
		m_sptrMvCamera->startDisplay((VR_HWND)m_lbShowWindow.GetSafeHwnd());
	}
	else
	{
		AfxMessageBox(_T("Start play failed!"));
		return;
	}

	// 控制相关按钮状态
	// Control related button status
	m_btnPlay.EnableWindow(FALSE);
	m_btnStopPlay.EnableWindow(TRUE);
	m_btnSaveConfig.EnableWindow(FALSE);
	m_btnTrigger.EnableWindow(TRUE);
}

void CCameraSubDlg::OnBnClickedButtonStopplay()
{
	if ( NULL != m_sptrMvCamera )
	{
		// TODO: 在TriggerMode开启情况下关闭SDK中似乎有死锁
		// 暂时先这么处理，后面再来跟这个问题
		// When triggermode is turned on, it seems that there is a deadlock in the SDK. For now, do this first, and then follow this problem
// 		procTriggerMode("Off");
// 		int iIndex = m_cmbTriggerMode.FindStringExact(0, _T("Off"));
// 		m_cmbTriggerMode.SetCurSel(iIndex);

		m_sptrMvCamera->stopDisplay();
	}	

	// 控制相关按钮状态
	// Control related button status
	m_btnPlay.EnableWindow(TRUE);
	m_btnStopPlay.EnableWindow(FALSE);
	m_btnSaveConfig.EnableWindow(TRUE);
	m_btnTrigger.EnableWindow(FALSE);
	m_bFirstTrigger = true;
}

void CCameraSubDlg::OnBnClickedButtonSaveconfig()
{
	Dahua::CMvCamera::ParamControlSetPtr sptrParamCtrlSet = m_sptrMvCamera->getParamControl();
	if ( NULL == sptrParamCtrlSet )
	{
		return;
	}

	bool bRet = false;

	do 
	{
		bRet = sptrParamCtrlSet->_sptrUserSetCtrl->saveUserSet();
		if ( !bRet )
		{
			break;
		}

		bRet = sptrParamCtrlSet->_sptrUserSetCtrl->setCurrentUserSet();
		
	} while (false);

	if ( !bRet )
	{
		AfxMessageBox(_T("Save config failed!"));
	}
}

void CCameraSubDlg::OnCbnSelchangeComboExposureauto()
{
	Dahua::CMvCamera::ParamControlSetPtr sptrParamCtrlSet = m_sptrMvCamera->getParamControl();
	if ( NULL == sptrParamCtrlSet )
	{
		return;
	}

	// 获取ExposureAuto节点对象
	// get exposureauto node object
	Dahua::GenICam::CEnumNode sptrEnumNode = sptrParamCtrlSet->_sptrAcquistionCtrl->exposureAuto();

	// 处理ComboBox控件参数设置
	// processing ComboBox control parameter settings
	procSelchangeCombo(sptrEnumNode, &m_cmbExposureAuto);
}

void CCameraSubDlg::OnCbnSelchangeComboExposuremode()
{
	Dahua::CMvCamera::ParamControlSetPtr sptrParamCtrlSet = m_sptrMvCamera->getParamControl();
	if ( NULL == sptrParamCtrlSet )
	{
		return;
	}

	// 获取ExposureMode节点对象
	// get exposureMode node object
	Dahua::GenICam::CEnumNode sptrEnumNode = sptrParamCtrlSet->_sptrAcquistionCtrl->exposureMode();

	// 处理ComboBox控件参数设置
	// processing ComboBox control parameter settings
	procSelchangeCombo(sptrEnumNode, &m_cmbExposureMode);
}

void CCameraSubDlg::OnCbnSelchangeComboGainauto()
{
	Dahua::CMvCamera::ParamControlSetPtr sptrParamCtrlSet = m_sptrMvCamera->getParamControl();
	if ( NULL == sptrParamCtrlSet )
	{
		return;
	}

	// 获取GainAuto节点对象
	// get GainAuto node object
	Dahua::GenICam::CEnumNode sptrEnumNode = sptrParamCtrlSet->_sptrAnalogCtrl->gainAuto();

	// 处理ComboBox控件参数设置
	// processing ComboBox control parameter settings
	procSelchangeCombo(sptrEnumNode, &m_cmbGainAuto);
}

void CCameraSubDlg::OnCbnSelchangeComboImageformat()
{
	Dahua::CMvCamera::ParamControlSetPtr sptrParamCtrlSet = m_sptrMvCamera->getParamControl();
	if ( NULL == sptrParamCtrlSet )
	{
		return;
	}

	// 获取PixelFormat节点对象
	// get PixelFormat node object
	Dahua::GenICam::CEnumNode sptrEnumNode = sptrParamCtrlSet->_sptrImageFormatCtrl->pixelFormat();

	// 处理ComboBox控件参数设置
	// processing ComboBox control parameter settings
	procSelchangeCombo(sptrEnumNode, &m_cmbImageFormat);
}

void CCameraSubDlg::OnCbnSelchangeComboTriggermode()
{
	TCHAR szBuf[24] = {0};
	int iIndex = m_cmbTriggerMode.GetCurSel();
	m_cmbTriggerMode.GetLBText(iIndex, szBuf);
	
#ifdef UNICODE
	std::string strBuf = WStringToString(szBuf);
#else
	std::string strBuf = szBuf;
#endif // UNICODE

	bool bRet = procTriggerMode(strBuf.c_str());
	if ( !bRet )
	{
		AfxMessageBox(_T("Set trigger mode failed!"));
	}	
}

void CCameraSubDlg::OnEnUpdateEditExposure()
{
	if ( NULL == m_sptrMvCamera )
	{
		return;
	}

	UpdateData(TRUE);

	Dahua::CMvCamera::ParamControlSetPtr sptrParamCtrlSet = m_sptrMvCamera->getParamControl();
	if ( NULL == sptrParamCtrlSet )
	{
		return;
	}

	// 获取ExposureTime节点对象
	// get ExposureTime node object
	Dahua::GenICam::CDoubleNode sptrDoubleNode = sptrParamCtrlSet->_sptrAcquistionCtrl->exposureTime();
	if ( !sptrDoubleNode.isValid() )
	{
		return;
	}

	if ( sptrDoubleNode.isWriteable() )
	{
		sptrDoubleNode.setValue(m_valExposureTime);
	}
}

void CCameraSubDlg::OnEnUpdateEditGain()
{
	if ( NULL == m_sptrMvCamera )
	{
		return;
	}

	UpdateData(TRUE);

	Dahua::CMvCamera::ParamControlSetPtr sptrParamCtrlSet = m_sptrMvCamera->getParamControl();
	if ( NULL == sptrParamCtrlSet )
	{
		return;
	}

	// 获取ExposureTime节点对象
	// get ExposureTime node object
	Dahua::GenICam::CDoubleNode sptrDoubleNode = sptrParamCtrlSet->_sptrAnalogCtrl->gainRaw();
	if ( !sptrDoubleNode.isValid() )
	{
		return;
	}

	if ( sptrDoubleNode.isWriteable() )
	{
		sptrDoubleNode.setValue(m_valGain);
	}
}

void CCameraSubDlg::OnEnUpdateEditFramerate()
{
	if ( NULL == m_sptrMvCamera )
	{
		return;
	}

	UpdateData(TRUE);

	Dahua::CMvCamera::ParamControlSetPtr sptrParamCtrlSet = m_sptrMvCamera->getParamControl();
	if ( NULL == sptrParamCtrlSet )
	{
		return;
	}

	// 获取ExposureTime节点对象
	// get ExposureTime node object
	Dahua::GenICam::CDoubleNode sptrDoubleNode = sptrParamCtrlSet->_sptrAcquistionCtrl->acquisitionFrameRate();
	if ( !sptrDoubleNode.isValid() )
	{
		return;
	}

	if ( sptrDoubleNode.isWriteable() )
	{
		sptrDoubleNode.setValue(m_valFrameRate);
	}
}

void CCameraSubDlg::OnDeltaposSpinExposure(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
	
	procDoubleSpinCtrl(pNMUpDown, m_valExposureTime, &m_editExposure);

	*pResult = 0;
}

void CCameraSubDlg::OnDeltaposSpinGain(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);

	procDoubleSpinCtrl(pNMUpDown, m_valGain, &m_editGain);
	
	*pResult = 0;
}

void CCameraSubDlg::OnDeltaposSpinFramerate(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);

	procDoubleSpinCtrl(pNMUpDown, m_valFrameRate, &m_editFrameRate);

	*pResult = 0;
}

void CCameraSubDlg::OnBnClickedButtonTrigger()
{
	Dahua::CMvCamera::ParamControlSetPtr sptrParamCtrlSet = m_sptrMvCamera->getParamControl();
	if ( NULL == sptrParamCtrlSet )
	{
		return;
	}

	// TriggerSoftware
	Dahua::GenICam::CCmdNode sptrTriggerSoftware = sptrParamCtrlSet->_sptrAcquistionCtrl->triggerSoftware();
	if ( !sptrTriggerSoftware.isValid() )
	{
		AfxMessageBox(_T("TriggerSoftware failed!"));
		return;
	}

	bool bRet = false;

	if ( m_bFirstTrigger )
	{
		do 
		{
			// TriggerSource
			Dahua::GenICam::CEnumNode sptrTriggerSource = sptrParamCtrlSet->_sptrAcquistionCtrl->triggerSource();
			if ( !sptrTriggerSource.isValid() )
			{
				break;
			}

			// TriggerSelector
			Dahua::GenICam::CEnumNode sptrTriggerSelector = sptrParamCtrlSet->_sptrAcquistionCtrl->triggerSelector();
			if ( !sptrTriggerSelector.isValid() )
			{
				break;
			}

			bRet = sptrTriggerSelector.setValueBySymbol("AcquisitionStart");
			if ( !bRet )
			{
				break;
			}

			bRet = sptrTriggerSoftware.execute();
			if ( !bRet )
			{
				break;
			}

			bRet = sptrTriggerSelector.setValueBySymbol("FrameStart");

		} while (false);

		if ( !bRet )
		{
			AfxMessageBox(_T("TriggerSoftware failed!"));
			return;
		}

		m_sptrMvCamera->enableRateCtrl(false);
	}

	bRet = sptrTriggerSoftware.execute();
	if ( !bRet )
	{
		AfxMessageBox(_T("TriggerSoftware failed!"));
	}

	return;
}

void CCameraSubDlg::OnBnClickedButtonForceip()
{
	if ( NULL != m_pForceIpDlg )
	{
		delete m_pForceIpDlg;
		m_pForceIpDlg = NULL;
	}

	m_pForceIpDlg = new CForceIpDlg();
	m_pForceIpDlg->setMvCamera(m_sptrMvCamera);
	m_pForceIpDlg->DoModal();

	initCameraIpInfo();
}

LRESULT CCameraSubDlg::OnMyDisconnectMsg(WPARAM wParam, LPARAM lParam)
{
	OnBnClickedButtonDisconnect();
	AfxMessageBox(_T("Camera is disconnected!!!"));

#if 0
	m_editGateway.SetWindowText(_T(""));
	m_editMask.SetWindowText(_T(""));
	m_editIp.SetWindowText(_T(""));
	enableWindowAll(FALSE);
	::PostMessage(GetParent()->GetSafeHwnd(), WM_MY_DISCOVERY_MESSAGE, NULL, NULL);
#endif
	return 0;
}

void CCameraSubDlg::OnOK()
{
	// 屏蔽关闭对话框操作
	// Shield close dialog action
}

BOOL CCameraSubDlg::PreTranslateMessage(MSG* pMsg)
{
	if ( pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE )
	{
		// 屏蔽ESC键关闭对话框
		// Shield ESCbutton close dialog action
		return TRUE;
	}

	return CDialog::PreTranslateMessage(pMsg);
}
